class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        // 按左边界排序，使区间尽可能重叠在一起
        sort(intervals.begin(), intervals.end());

        int n = intervals.size(), left = intervals[0][0], right = intervals[0][1], ret = 0;
        for(int i = 1; i < n; i++)
        {
            if(intervals[i][0] < right)
            {
                // 区间重叠
                ret++;      // 统计重叠区间
                right = min(right, intervals[i][1]);    // 更新右边界
            }
            else
            {
                // 区间不重叠
                // 更新当前左右区间
                left = intervals[i][0];
                right = intervals[i][1];
            }
        }
        return ret;
    }
};